package com.apes.project.jekunAuto.timeTask.car;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.pi.service.PiManager;
import com.apes.project.jekunAuto.timeTask.BaseService;
import com.apes.scm.masterdata.car.model.*;
import com.apes.scm.masterdata.car.repository.*;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import com.apestech.framework.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;


@Service("carSynService")
public class CarSynService extends BaseService {


    @Autowired
    CarLabelRepository carLabelRepository;

    @Autowired
    CarModelRepository carModelRepository;

    @Autowired
    CarConfigurationRepository carConfigurationRepository;

    @Autowired
    CarModelServiceLevelRepository carModelServiceLevelRepository;

    @Autowired
    CarServiceLevelRepository carServiceLevelRepository;
    @Autowired
    MasCarBrandRepository masCarBrandRepository;

    @Autowired
    CarTypeRepository carTypeRepository;

    @Autowired
    EmissionStandardRepository emissionStandardRepository;

    PiManager piManager;

    @Autowired
    void setPiManager(PiManager piManager) {
        this.piManager = piManager;
    }

    /*
     *  同步汽车厂牌数据
     * */
    public JSONArray synCarLabels(SimpleRequest simpleRequest) {
        return piManager.timedTaskSynErp("ipmQuerybmxxService.queryQCCP");
    }

    public CarLabel saveCarLabel(SimpleRequest simpleRequest) {
        CarLabel carLabel = simpleRequest.getO(CarLabel.class);

        JSONObject simpleRequestObj = simpleRequest.getJO();
        // 获取厂牌类型
        String QCCPLY = simpleRequestObj.getString("QCCPLY");
        if (QCCPLY != null) {
            Enumeration carCategory = new Enumeration();
            if ("0".equals(QCCPLY)) {
                carCategory.setId("000194");
            } else if ("1".equals(QCCPLY)) {
                carCategory.setId("000195");
            } else if ("2".equals(QCCPLY)) {
                carCategory.setId("000196");
            }
            carLabel.setCarCategory(carCategory);
        }

        String QCPPID = simpleRequestObj.getString("QCPPID");
        if (QCPPID != null) {
            MasCarBrand carBrand = masCarBrandRepository.findOne(QCPPID);
            if (carBrand == null) {
                carBrand = new MasCarBrand();
                carBrand.setId(QCPPID);
            }
            carBrand.setValid(simpleRequestObj.getBoolean("YXBJ"));
            carBrand.setName(simpleRequestObj.getString("QCPPMC"));
            masCarBrandRepository.saveAndFlush(carBrand);
            carLabel.setMasCarBrand(carBrand);
        }
        // 查询数据是否已经存在， 已经存在拿version字段
        CarLabel carLabelOld = carLabelRepository.findOne(carLabel.getId());
        if (carLabelOld != null) {
            carLabel.setVersion(carLabelOld.getVersion());
        }
        return carLabelRepository.saveAndFlush(carLabel);
    }


    public JSONArray synCarModels(SimpleRequest request) {
        return getData("ipmQuerybmxxService.queryCHX");
    }

    public CarModel saveCarModel(SimpleRequest simpleRequest) {
        CarModel carModel = simpleRequest.getO(CarModel.class);

        JSONObject simpleRequestObj = simpleRequest.getJO();
        //获取厂牌编码
        String QCCPID = simpleRequestObj.getString("QCCPID");

        if (QCCPID != null) {
            CarLabel carLabel = carLabelRepository.findOne(QCCPID);
            carModel.setCarLabel(carLabel);
        }
        // 查询数据是否已经存在， 已经存在拿version字段
        CarModel carModel01 = carModelRepository.findOne(carModel.getId());
        if (carModel01 != null) {
            carModel.setVersion(carModel01.getVersion());
        }
        return carModelRepository.saveAndFlush(carModel);
    }


    /*
     *  同步配置车型信息
     * */
    public JSONArray synCarQCPZ(SimpleRequest simpleRequest) {
        return piManager.timedTaskSynErp("ipmVipService.getCarType");
    }


    /**
     * 保存配置配置车型信息
     *
     * @param simpleRequest
     * @return
     */
    public CarConfiguration saveCarQCPZ(SimpleRequest simpleRequest) {

        CarConfiguration carQCPZ = simpleRequest.getO(CarConfiguration.class);

        JSONObject simpleRequestObj = simpleRequest.getJO();
        // 获取厂牌类型
        String QDFS = simpleRequestObj.getString("QDFS");//驱动方式
        String FDJQTJS = simpleRequestObj.getString("FDJQTJS");//发动机启停技术

        String QCCPID = simpleRequestObj.getString("QCCPID");//厂牌编码
        String CHXID = simpleRequestObj.getString("CHXID");//车型编码
        String QCLXID = simpleRequestObj.getString("QCLXID");//车辆类型
        String PFBZID = simpleRequestObj.getString("PFBZID");//排放标准编码

        CarLabel carLabel = new CarLabel();

        if (QCCPID!=null){
            carLabel = carLabelRepository.findOne(QCCPID);
            if (carLabel==null){
                carLabel = new CarLabel();
                carLabel.setId(QCCPID);
                carLabel.setName(simpleRequestObj.getString("QCCPMC"));
                carLabel.setManufacturers(simpleRequestObj.getString("QCCS"));
                carLabel.setCarBrand(simpleRequestObj.getString("QCPPMC"));
                carLabel = carLabelRepository.saveAndFlush(carLabel);
            }
        }

        CarModel carModel = new CarModel();
        if (CHXID!=null){
            carModel = carModelRepository.findOne(CHXID);
            if (carModel==null){
                carModel = new CarModel();
                carModel.setId(CHXID);
                carModel.setName(simpleRequestObj.getString("CHXMC"));
                carModel.setCarLabel(carLabel);
                carModel = carModelRepository.saveAndFlush(carModel);
            }
        }

        if (!StringUtil.isEmpty(QCLXID)) {
            CarType carType = carTypeRepository.findOne(QCLXID);
            if (carType==null){
                carType = new CarType();
                carType.setId(QCLXID);
                if (simpleRequestObj.getString("QCLXMC")!=null)carType.setName(simpleRequestObj.getString("QCLXMC"));
                carType = carTypeRepository.saveAndFlush(carType);
            }
            carQCPZ.setCarType(carType);
        }


        if (!StringUtil.isEmpty(PFBZID)) {
            EmissionStandard emissionStandard = emissionStandardRepository.findOne(PFBZID);
            if (emissionStandard==null){
                emissionStandard = new EmissionStandard();
                emissionStandard.setId(PFBZID);
                if (simpleRequestObj.getString("PFBZMC")!=null)emissionStandard.setName(simpleRequestObj.getString("PFBZMC"));
                emissionStandardRepository.saveAndFlush(emissionStandard);
            }
            carQCPZ.setEmissionStandard(emissionStandard);
        }


        carQCPZ.setCarLabel(carLabel);
        carQCPZ.setCarModel(carModel);

        if (QDFS != null) {
            Enumeration drive = new Enumeration();
            String driveId = null;
            if ("0".equals(QDFS)) {
                driveId="000201";
            } else if ("1".equals(QDFS)) {
                driveId="000202";
            } else if ("2".equals(QDFS)) {
                driveId="000203";
            } else if ("3".equals(QDFS)) {
                driveId="000204";
            } else if ("4".equals(QDFS)) {
                driveId="000205";
            } else if ("5".equals(QDFS)) {
                driveId="000206";
            }else if ("6".equals(QDFS)) {
                driveId="001227";
            }
            if (driveId!=null){
                drive = SpringManager.getBean(EnumerationRepository.class).findOne(driveId);
                carQCPZ.setDrive(drive);
            }
        }

        //发动机启停技术
        if (FDJQTJS != null) {
            boolean fdjqt = true;
            if ("没有".equals(FDJQTJS)||"无".equals(FDJQTJS)) {
                fdjqt = false;
            }
            carQCPZ.setSTT(fdjqt);
        }

        carQCPZ.setNK(simpleRequestObj.getInteger("NK"));
        carQCPZ.setYJFS(simpleRequestObj.getString("YJFS"));
        String validFlag = simpleRequestObj.getString("YXBJ");
        if (validFlag != null) {
            carQCPZ.setValidFlag(validFlag.equals("0") ? "invalid" : (validFlag.equals("1") ? "valid" : "stopService"));
        }
        // 查询数据是否已经存在， 已经存在拿version字段
        CarConfiguration CarConfiguration = carConfigurationRepository.findOne(carQCPZ.getId());
        if (CarConfiguration != null) {
            carQCPZ.setVersion(CarConfiguration.getVersion());
        }
        return carConfigurationRepository.saveAndFlush(carQCPZ);
    }

    /*
     *  同步车型服务等级
     * */
    public JSONArray synCarModelServiceLevel(SimpleRequest simpleRequest) {
        return piManager.timedTaskSynErpByConditions("ipmVipService.getCXFWDJ","CLFWDJID");
    }

    public CarModelServiceLevel saveCarModelServiceLevel(SimpleRequest simpleRequest) {

        JSONObject requestJO = simpleRequest.getJO();
        String carServiceLevelId = requestJO.getString("CLFWDJID");
        String carModelId = requestJO.getString("CHXID");
        CarModelServiceLevel carModel = carModelServiceLevelRepository.findOne(JpaDsl.toCriteriaByEq("carModel.id", carModelId, "carServiceLevel.id", carServiceLevelId)).orElse(new CarModelServiceLevel());
        carModel.setCarServiceLevel(carServiceLevelRepository.findOne(carServiceLevelId));
        carModel.setCarModel(carModelRepository.findOne(carModelId));
        carModel.setMaintenanceInterval(requestJO.getInteger("BYJGSJ"));
        carModel.setMaintenanceMileage(requestJO.getInteger("DQBYLC"));
        return carModelServiceLevelRepository.saveAndFlush(carModel);
    }
}
